#ifndef CX_TASK_H
#define CX_TASK_H
#include "cx_typedef.h"
#include "cx_list.h"
#include "cx_os_config.h"
#include "cx_os_time.h"
#include "cx_os_critical.h"
typedef unsigned int StackType_t;

#define INITAL_XPSR 0x01000000
#define ENTRY_ADDRESS_MASK ((StackType_t) 0xfffffffeUL)

/*
*????�STM32F10xxx Cortex-M3 programming manual�4.4.3,????�PM0056�????????
* ?Cortex-M?,????SCB?SHPR3???????SysTick?PendSV??????
* System handler priority register 3 (SCB_SHPR3) SCB_SHPR3:0xE000 ED20
* Bits 31:24 PRI_15[7:0]: Priority of system handler 15, SysTick exception
* Bits 23:16 PRI_14[7:0]: Priority of system handler 14, PendSV
*/
#define portNVIC_SYSPRI3_REG         (*(( volatile cx_uint32_t *) 0xe000ed20))
#define	configKERNEL_INTERRUPT_PRIORITY (0xff00)
#define portNVIC_PENDSV_PRI (((cx_uint32_t) configKERNEL_INTERRUPT_PRIORITY ) << 16UL)
#define portNVIC_SYSTICK_PRI (((cx_uint32_t) configKERNEL_INTERRUPT_PRIORITY ) << 24UL )

#define TaskMinimalStack 128

// #define TASK1_STACK_SIZE                    128
// StackType_t Task1Stack[TASK1_STACK_SIZE];
// #define TASK2_STACK_SIZE                    128
// StackType_t Task2Stack[TASK2_STACK_SIZE];


typedef struct TaskControlBlock
{
	volatile StackType_t *StackOfTop;
	StackType_t *pStack;
	void *ProgEntry;

	int priority;
	unsigned long long eta_delay_ticks;
	cx_slist_item_t owner;
}TCB_t;

typedef enum
{
	TASK_ERR_OK,
	TASK_ERR_INITIALIZE_PRIORITY_EXCEED
}task_err_t;


task_err_t TaskCreat(TCB_t *tcb,void* ProgEntry, void* StackBuffer, unsigned int StackDepth,int prio);
void StartSchedual(void);
void TaskYild(void);
void TaskDelay(unsigned long long ticks);
#endif